:- module(dump_conecta2amos,[main/0]).

:- use_module(library(write)).
:- use_module(library(lists)).

:- use_module(amos('Insertion_form/lc_database_impl'), [get_package/7,term_desc/3]).

:- use_module(db(resource_access)).
:- use_module(db(asset_access)).
:- use_module(db(dictionary_access)).
:- use_module(db(organization_access)).
:- use_module(db(database_access)).
:- use_module(db(database_impl),[update_db/0]).
:- use_module(db(user_access)).
:- use_module(library(lists),[append/3]).

main:- 
	display('-------------------------------------------------------------'),nl,
	display('Dumping data from Conecta simplified database to Amos '),nl,
	display('database.'),nl,
	display('-------------------------------------------------------------'),nl,
	populate_dictionary,
	populate_packages,
	update_db,
	display('-------------------------------------------------------------'),nl,
	display('Database updated. If it is a persdb database, check the'),nl,
	display('permissions of the database files'),nl,
	display('-------------------------------------------------------------'),nl.

populate_packages:-
	get_package_data(Package,Desc,Requires,Provides,Link,Size,Lic),
	create_resource(Package,Desc,Requires,Provides,Link,Size,Lic,Res),
	display('Asserting package: '),display(Package),nl,
	database_access:resource_db_update(_,Res),
	fail.
populate_packages.


get_package_data(Name,Desc,Requires,Provides,Link,Size,Lic):-
	get_package(Name,Provides,Requires,Link,Desc,Size,Lic).


create_resource(Package,Desc,Requires,Provides,Link,Size,Lic,Res):-
	resource(Res),
	gen_ditem_list(Provides,DProvides,WP),
	resource_identificationTags(Res,DProvides,Res),
	gen_ditem_list(Requires,DRequires,WR),
	resource_requires(Res,DRequires,Res),
%
	asset(Ass),
	asset_name(Ass,Package,Ass),
	asset_download_page(Ass,Link,Ass),
	asset_license(Ass,Lic,Ass),
	asset_version(Ass,none,Ass),
	asset_description(Ass,[Desc],Ass),
%
	organization(OrgSubmittedby),
	organization_name(OrgSubmittedby,none,OrgSubmittedby),
	organization_check(OrgSubmittedby,WO),
	user(Submittedby),
	user_affiliation(Submittedby,OrgSubmittedby,Submittedby),
	user_check(Submittedby,WU),
	asset_submitted_by(Ass,Submittedby,Ass),
	asset_author(Ass,[Submittedby],Ass),
	asset_submission_date(Ass,'01/01/2003',Ass),
	asset_check(Ass,WA),
%
	resource_asset(Res,Ass,Res),
	convert_type(Size,int,SizeInt),
	resource_size(Res,SizeInt,Res),
	resource_check(Res,WRs),
%
	!,
	flatten([WP,WR,WA,WRs,WO,WU],Errors),
	( Errors = [] ->
	  true
	; 
	  display('Errors in package: '),display(Package),nl,
	  display('                   '),display(Errors),nl,
	  fail
	).

gen_ditem_list([],[],[]):- !.
gen_ditem_list([A|As],[DitemStream|Ditems],Errs):-
	 ditem(Ditem),
	 ( functor(A,F,1) ->
	   arg(1,A,A0),
	   ditem_entry(Ditem,A0,Ditem),
	   functor(DitemStream,F,1),
	   arg(1,DitemStream,Ditem)
	 ; ditem_entry(Ditem,A,Ditem),
	   DitemStream = Ditem
	 ),
	 (
	     (ditem_description(Ditem, _), Err1 = [])
	 ;
	     ditem_check(Ditem,Err1)
	 ),	  
	 gen_ditem_list(As,Ditems,Err2),
	 append(Err1,Err2,Errs),!.

%%--------------------------------------------------------------------

populate_dictionary:-
	get_dict_data(Term,Desc,Generalizations),
	create_ditem(Term,Desc,Generalizations,Ditem),
	display('Asserting dictionary item: '),display(create_ditem(Term,Desc,Generalizations,Ditem)),nl,
	database_access:ditem_db_update(_,Ditem),
	fail.
populate_dictionary.


get_dict_data(Term,Desc,Generalizations):-
	term_desc(Term,Desc,Generalizations).

create_ditem(Term,Desc,Generalizations,Ditem):-
	ditem(Ditem0),
	ditem_entry(Ditem0,Term,Ditem0),
%
	( ditem_description(Ditem0,_) ; true ),
	ditem_definition(Ditem0,Desc,Ditem1),
	gen_ditem_list(Generalizations,DGeneralizations,WG),
	ditem_generalization(Ditem1,DGeneralizations,Ditem),
	ditem_check(Ditem,WD),
%
	!,
	flatten([WG,WD],Errors),
	( Errors = [] ->
	  true
	; 
	  display('Errors in dictionary item: '),display(Term),nl,
	  display('                           '),display(Errors),nl,
	  fail
	).
	

update_ditem_list([]).
update_ditem_list([D|Ds]):-
	ditem_db_update(_,D),
	update_ditem_list(Ds).

%%--------------------------------------------------------------------

convert_type(Atm,int,Atm):- 
	int(Atm),!.
convert_type('',int,0).
convert_type(Atm,int,Val):-
	atm(Atm),
	atom_codes(Atm,SVal),
	number_codes(Val,SVal), !.


flatten([],[]).
flatten([M|Ls],Flattened):-
	list(M), !,
	flatten(Ls,LsFlattened),
	append(M,LsFlattened,Flattened).
flatten([M|Ls],Flattened):-
	flatten(Ls,LsFlattened),
	Flattened = [M | LsFlattened].

